const api = require('../../api/api')
const app = getApp()
const util = require('../../utils/js/util')
Page({
  data: {
    id: '',
    safebottom: util.getSafeToBottom(),
    list: [],
    listStatus: 'loading',
    page: 1,
    isUpOrder: false,
  },
  onLoad(options) {
    this.setData({ id: options.id })
    this.getPatient()
  },
  onReachBottom() {
    if (this.data.listStatus === 'loading') {
      this.setData({ page: this.data.page + 1 })
      this.getClientList()
    }
  },
  // 跳过
  jump() {
    if(this.data.isUpOrder == false) {
      this.setData({ isUpOrder: true })
      wx.navigateTo({ url: "../purchased/index?id=" + this.data.id + '&type=check' })
    }
  },
  // 选择就诊人
  async selectPerson({ currentTarget: { dataset: { item } } }) {
    const res = await api.checkUpdateOrder({ id: this.data.id, client_id: item.id })
    util.toast('下单成功')
    setTimeout(() => wx.navigateTo({ url: '../purchased/index?id=' + this.data.id + '&type=check' }), 500);
  },
  // 管理就诊人
  patientAdmin() {
    wx.navigateTo({ url: '../patient/index' })
  },
  // 获取就诊人列表
  async getPatient() {
    const res = await api.getClientList({ p: this.data.page })
    const newdata = res.data.map(v => ({ ...v, age: new Date().getFullYear() - new Date(v.birthday).getFullYear() }))
    this.setData({
      list: this.data.page == 1 ? newdata : this.data.list.concat(newdata),
      listStatus: res.last_page > res.current_page ? 'loading' : res.last_page == res.current_page ? 'tobottom' : res.last_page < res.current_page ? 'nodata' : ''
    })
  },
})
